﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Expressing_Label;

namespace Expressing_Label
{
    public partial class ConfigurationFrm : Form
    {
        private bool c_Movingbl = false;
        private Point c_CurrentPt= new Point();
        public static bool g_blSendByMiFrm = false;
        public static int g_iNextFrm = 0;
        private Form c_frmShowing = null;
        public static int g_ySQLButton = 0;
        public static int g_yVideoButton = 0;
        public static int g_yExpressingButton = 0;
        public static int g_yExperimentButton = 0;
        

        public ConfigurationFrm()
        {
            InitializeComponent();
            g_ySQLButton = Sqlcheck_btn.Top + Sqlcheck_btn.Height / 2;
            g_yVideoButton = VideoCnt_btn.Top + VideoCnt_btn.Height / 2;
            g_yExpressingButton = Expressing_btn.Top + Expressing_btn.Height / 2;
            g_yExperimentButton = btnExperiment.Top + btnExperiment.Height / 2;
            InitializeButton();
        }

        private void InitializeButton()
        {
            foreach (Control c in this.Controls)
            {
                if (c is Button)
                {
                    Button b = (c as Button);
                    b.FlatStyle = FlatStyle.Flat;
                    b.FlatAppearance.BorderSize = 0;
                    b.FlatAppearance.MouseDownBackColor = Color.PaleTurquoise;
                    b.FlatAppearance.MouseOverBackColor = Color.LightCyan;
                    b.Paint += new PaintEventHandler(Button_Paint);
                }
            }
        }

        private void Button_Paint(object sender, PaintEventArgs e)
        {
            Button b = (Button)sender;
            GraphicsPath gp = CalculateGraphicPaht((Bitmap)b.BackgroundImage);
            b.Region = new Region(gp);
        }

        private GraphicsPath CalculateGraphicPaht(Bitmap bp)
        {
            GraphicsPath gp = new GraphicsPath();
            Color colorBeginning = bp.GetPixel(0, 0);
            for (int i = 0; i < bp.Height; i++)
            {
                for (int j = 0; j < bp.Width; j++)
                {
                    if (bp.GetPixel(j, i) != colorBeginning)
                    {
                        for (int jNext = j; jNext < bp.Width; jNext++)
                            if (bp.GetPixel(jNext, i) == colorBeginning)
                            {
                                gp.AddRectangle(new Rectangle(j, i, jNext - j, 1));
                                j = jNext;
                                break;
                            }
                    }
                }
            }
            return gp;
        }

        //private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)
        //{

        //    GraphicsPath graphicsPath = new GraphicsPath();


        //    Color colorTransparent = bitmap.GetPixel(0, 0);

        //    int colOpaquePixel = 0;

        //    for (int row = 0; row < bitmap.Height; row++)
        //    {

        //        colOpaquePixel = 0;

        //        for (int col = 0; col < bitmap.Width; col++)
        //        {

        //            if (bitmap.GetPixel(col, row) != colorTransparent)
        //            {

        //                colOpaquePixel = col;

        //                int colNext = col;

        //                for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)
        //                    if (bitmap.GetPixel(colNext, row) == colorTransparent)
        //                        break;

        //                graphicsPath.AddRectangle(new Rectangle(colOpaquePixel,
        //                 row, colNext - colOpaquePixel, 1));

        //                col = colNext;
        //            }
        //        }
        //    }

        //    return graphicsPath;
        //}

        private void Experiment_btn_Click(object sender, EventArgs e)
        {
            ShowForm_Mtd(btnExperiment.TabIndex);
        }

        private void Sqlcheck_btn_Click(object sender, EventArgs e)
        {
            ShowForm_Mtd(Sqlcheck_btn.TabIndex);
        }

        private void VideoCnt_btn_Click(object sender, EventArgs e)
        {
            ShowForm_Mtd(VideoCnt_btn.TabIndex);

        }

        private void Expressing_btn_Click(object sender, EventArgs e)
        {
            ShowForm_Mtd(Expressing_btn.TabIndex);

        }

        private void ConfigurationFrm_MouseDown(object sender, MouseEventArgs e)
        {
            c_Movingbl = true;
            c_CurrentPt = MousePosition;
        }

        private void ConfigurationFrm_MouseMove(object sender, MouseEventArgs e)
        {
            if(c_Movingbl)
            {
                int x = MousePosition.X - c_CurrentPt.X;
                int y = MousePosition.Y - c_CurrentPt.Y;
                this.Left += x;
                this.Top += y;
                foreach(var item in this.OwnedForms)
                {
                    if (item != null)
                    {
                        item.Left += x;
                        item.Top += y;
                    }
                }
                c_CurrentPt = MousePosition;
            }
        }

        private void ConfigurationFrm_MouseLeave(object sender, EventArgs e)
        {
            c_Movingbl = false;
        }

        private void ConfigurationFrm_MouseUp(object sender, MouseEventArgs e)
        {
            c_Movingbl = false;
        }

        private void ConfigurationFrm_Load(object sender, EventArgs e)
        {
            g_blSendByMiFrm = false;
            if (PublicResources.g_iConfiguraState == 1)
            {
                ShowForm_Mtd(Sqlcheck_btn.TabIndex);
                VideoCnt_btn.Enabled = false;
                Expressing_btn.Enabled = false;
                btnExperiment.Enabled = false;
            }
        }

        private void ConfigurationFrm_Activated(object sender, EventArgs e)
        {
            if (g_blSendByMiFrm)
            {
                ShowForm_Mtd(g_iNextFrm);
                if (PublicResources.g_iConfiguraState == g_iNextFrm)
                    PublicResources.g_iConfiguraState++;
                foreach (Control c in this.Controls)
                {
                    if (c is Button && (c as Button).TabIndex == g_iNextFrm)
                    {
                        (c as Button).Enabled = true;
                    }
                }
                g_blSendByMiFrm = false;
            }
        }

        private void ShowForm_Mtd(int index)
        {
            if (c_frmShowing != null)
                c_frmShowing.Close();
            switch (index)
            {
                case 0:
                    c_frmShowing = new SQLCheckMiFrm();
                    break;
                case 1:
                    c_frmShowing = new VideoContentMiFrm();
                    break;
                case 2:
                    c_frmShowing = new ExpressingMiFrm();
                    break;
                case 3:
                    c_frmShowing = new ExperimentMiFrm();
                    break;
            }
            c_frmShowing.Owner = this;
            c_frmShowing.Show();   
        }

    }
}
